cap cd "/Users/ddgueorg/Dropbox/C-GAP/C-GAP Replication Files/Coding"
cap cd "C:\Users\ejm5\Dropbox\C-GAP\C-GAP Replication Files\Coding"

clear all
set more off 
*****

**Data Composition**
cd "../Data"


#delimit;
set mem 500m;
use QoG.dta;
merge 1:1 cname year using obi.dta;
drop _merge;
sort cname year;
merge 1:1 cname year using HRV2013.dta;


#delimit;
generate pfi_rescale=.;
foreach num of numlist 2002(1)2012 {;
sum rsf_pfi if year==`num';
replace pfi_rescale= 100-[99*((rsf_pfi-r(min))/(r(max)-r(min)))+0] if year==`num';
};


#delimit;

rename transparencyindex hrv;
drop if ccode==.;
xtset ccode year;
replace gir_acs=l.gir_acs if gir_acs==.;
#delimit;
xtset ccode year;
replace hrv=l.hrv if year==2011;
replace upper_bound=l.upper_bound if year==2011;
replace lower_bound=l.lower_bound if year==2011;
replace hrv=l.hrv if year==2012;
replace upper_bound=l.upper_bound if year==2012;
replace lower_bound=l.lower_bound if year==2012;
 



xtset ccode year;
replace ht_regtype1=l.ht_regtype1 if year==2011;
replace ht_regtype1=l.ht_regtype1 if year==2012;
by ht_regtype1 year, sort: egen gir_csmai_ht=mean(gir_csmai);
by ht_regtype1 year, sort: egen gir_acs_ht=mean(gir_acs);
by ht_regtype1 year, sort: egen  pfi_rescale_ht=mean( pfi_rescale);
by ht_regtype1 year, sort: egen  hrv_ht=mean(hrv);

xtset ccode year;
replace chga_demo=l.chga_demo if year==2009;
replace chga_demo=l.chga_demo if year==2010;
replace chga_demo=l.chga_demo if year==2011;
replace chga_demo=l.chga_demo if year==2012;
by chga_demo year, sort: egen gir_csmai_chga=mean(gir_csmai);
by chga_demo year, sort: egen gir_acs_chga=mean(gir_acs);
by chga_demo year, sort: egen  pfi_rescale_chga=mean( pfi_rescale);
by chga_demo year, sort: egen  hrv_chga=mean(hrv);

#delimit
by ht_regtype1 year, sort: egen  obi_ht=mean(obi);
by chga_demo year, sort: egen obi_chga=mean(obi);

#delimit;
preserve;
drop if hrv==.;
gsort - hrv;
by year , sort: gen hrv_rank=_n;
list hrv hrv_rank year if ccode==156 & year==1980;
list hrv hrv_rank year if ccode==156 & year==2008;
list hrv hrv_rank year if ccode==156 & year==2010;
tabstat hrv if year>=1980, by(year) stat(n);
restore;


#delimit;
preserve;
drop if pfi_rescale==.;
gsort -  pfi_rescale;
by year, sort: gen pfi_rank=_n;


#delimit;
list pfi_rescale pfi_rank year if ccode==156 & year==2002;
list  pfi_rescale pfi_rank year if ccode==156 & year==2008;
list  pfi_rescale pfi_rank year if ccode==156 & year==2012;
list pfi_rescale cname pfi_rank if year==2002;
list pfi_rescale cname pfi_rank if year==2008;
list pfi_rescale cname pfi_rank if year==2012;
tabstat pfi_rescale if year>=2002, by(year) stat(n);
restore;

#delimit;
preserve;
drop if obi==.;
gsort -  obi;
by  year , sort: gen obi_rank=_n;
restore;

#delimit;
preserve;
drop if gir_acs==.;
gsort -  gir_acs;
by  year , sort: gen gir_rank=_n;
restore;


/*Begin results output*/
cd "../Results/";

#delimit;
twoway (rcap lower_bound upper_bound year if ccode==156 & year>=1980, lcolor(gs4) msize(medsmall))
		(scatter hrv year if ccode==156 & year>=1980, mcolor(gs4) msymbol(triangle))
		(rcap lower_bound upper_bound year if ccode==704 & year>=1980, lcolor(gs8) lpattern(dash) msize(vsmall))
		(scatter hrv year if ccode==704 & year>=1980, mcolor(gs8) msymbol(diamond) msize(vsmall))
		(rcap lower_bound upper_bound year if ccode==840 & year>=1980, lcolor(gs12) lpattern(shortdash) )
		(scatter hrv year if ccode==840 & year>=1980, mcolor(gs12) msymbol(square) msize(medsmall)), 
ytitle("Transparency Index", size(medsmall) margin(medium)) ylab(-2(1)6, labsize(medsmall)) xtitle("") xlab(1980(2)2012, labsize(vsmall))
legend(size(vsmall) rows(3) label(1 China) label(2 "") label(3 Vietnam) label(4 "") label(5 USA) label(6 "") position(11) ring(0)) scheme(s1mono);
graph save "fig2_3_hrv.gph", replace;
graph export "fig2_3_hrv.pdf", as(pdf) replace;


#delimit;
lab var pfi_rescale "Press Freedom Index";
lab var hrv "Transparency Index";
lab var obi "Open Budget Index";
lab var gir_acs "Administrative & Civil Service Subindex";

#delimit;
generate lab_china=ccodealp if ccode==156;

#delimit;
graph matrix pfi_rescale obi hrv gir_acs if year==2012 & hrv>-2,  
ylabel(0(50)100, labsize(vsmall) axis(2)) ylabel(0(5)10, labsize(vsmall) axis(3)) ylabel(0(50)100, labsize(vsmall) axis(4)) 
xlabel(0(50)100, labsize(vsmall) axis(1)) xlabel(0(50)100, labsize(vsmall) axis(2)) xlabel(0(50)10, labsize(vsmall) axis(3))
half mlabel(lab_china) mlabsize(vsmall) mlabcolor(black) msymbol(diamond) mcolor(gs8) msize(vsmall) scheme(s2mono)
xlab(, labsize(small));
graph save "fig2_2_scatter.gph", replace;
graph export "fig2_2_scatter.pdf", as(pdf) replace;




#delimit;
twoway (line gir_acs year if ccode==156 & year>=2007 & year<=2012, lcolor(black) lwidth(thick) )
	(scatter gir_acs year if ccode==156 & year>=2007, mcolor(black) msymbol(diamond) msize(medium)) 
(line gir_acs year if ccode==704 & year>=2007 & year<=2012, lcolor(gs12)  lwidth(medthick))
(line gir_acs year if ccode==840 & year>=2007 & year<=2012, lcolor(navy) lpattern (dash) lwidth(medthick))
(line gir_acs_ht year if ht_regtype1==3 & year>=2007 & year<=2012, lcolor(gs7) lpattern (dash) lwidth(thick))
(line gir_acs_chga year if chga_dem==0 & year>=2007 & year<=2012, lcolor(gs12) lpattern (shortdash) lwidth(thick))
, xlab(2007(1)2012, labsize(medsmall)) ylab(0(20)100, labsize(medsmall)) yline(0 100, lcolor(gs14) lwidth(thick)) 
ytitle("Adminstrative and Civil Service Subindex", size(medium) margin(medium)) xtitle("")
legend(size(vsmall) label(1 "China Trend") label(2 "China Marker")  label(3 Vietnam) label(4 USA) label(5 All Single Party) label(6 All Nondemocracies)
position(5) ring(0)) scheme(s1mono);
graph save "fig2_4_giracs.gph", replace;
graph export "fig2_4_giracs.pdf", as(pdf) replace;




















